﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using Common;

namespace AnesSystem.ViewModels
{
    class PostoperativeAnalgesiaCtrlViewModel : BaseViewModel<PostoperativeAnalgesiaCtrlViewModel>
    {
        #region Variables
        private PostoperativeAnalgesiaModel _postoperativeAnalgesiaCopy;
        private PostoperativeAnalgesiaModel _postoperativeAnalgesia;
        private ObservableCollection<PostoperativeAnalgesiaElement> _ctrlList;
        private string _ztfsSelect;
        List<string> _ztfsStr = new List<string>() { "PCIA", "PCEA", "PCNA" };
        List<TextBlock> _shztXianShi;


        #endregion

        #region Constructor
        public PostoperativeAnalgesiaCtrlViewModel()
        {
            //_patientLink = DataManager.Instance.CurrentPatientLink;
            //_patientDrugsModelCopy = new PatientDrugsModel();
            _ctrlList = new ObservableCollection<PostoperativeAnalgesiaElement>();
            _shztXianShi = new List<TextBlock>();
        }

        #endregion

        #region Properties
        

        public PostoperativeAnalgesiaModel PostoperativeAnalgesiaCopy
        {
            get
            {
                return _postoperativeAnalgesiaCopy;
            }

            set
            {
                _postoperativeAnalgesiaCopy = value;
                RaisePropertyChanged("PostoperativeAnalgesiaCopy");
            }
        }

        public PostoperativeAnalgesiaModel PostoperativeAnalgesia
        {
            get
            {
                return _postoperativeAnalgesia;
            }

            set
            {
                _postoperativeAnalgesia = value;
                if (value != null)
                {
                    PostoperativeAnalgesiaCopy = value.ObjectToJsonM().JsonToObjectM<PostoperativeAnalgesiaModel>();
                }
                RaisePropertyChanged("PostoperativeAnalgesia");
            }
        }

        public ObservableCollection<PostoperativeAnalgesiaElement> CtrlList
        {
            get
            {
                return _ctrlList;
            }

            set
            {
                _ctrlList = value;
                RaisePropertyChanged("CtrlList");
            }
        }

        public List<string> ZtfsStr
        {
            get
            {
                return _ztfsStr;
            }

            set
            {
                _ztfsStr = value;
                RaisePropertyChanged("ZtfsStr");
            }
        }

        public List<TextBlock> ShztXianShi
        {
            get
            {
                return _shztXianShi;
            }

            set
            {
                _shztXianShi = value;
                RaisePropertyChanged("ShztXianShi");
            }
        }

        public string ZtfsSelect
        {
            get
            {
                return _ztfsSelect;
            }

            set
            {
                if (value == null) return;
                _ztfsSelect = value;
                SelectSinceFilling();
                RaisePropertyChanged("ZtfsSelect");
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event
        
        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods
        public void AddPosCtrl()
        {
            PostoperativeAnalgesiaElement ctrl = new PostoperativeAnalgesiaElement();
            ItemModel itemDrug = new ItemModel();
            ctrl.Item = itemDrug;
            PostoperativeAnalgesia.ItemList.Add(ctrl.Item);
            ctrl.ConfigUnitsModelList = DataManager.Instance.ConfigUnitsModelList.ToList();
            CtrlList.Add(ctrl);
        }

        public ItemModel CreateMedicineTtem(int num, ItemModel itemDrug)
        {
            PostoperativeAnalgesiaElement ctrl = new PostoperativeAnalgesiaElement();
            ctrl.Item = itemDrug;
            ctrl.ConfigUnitsModelList = DataManager.Instance.ConfigUnitsModelList.ToList();
            CtrlList.Add(ctrl);
            return ctrl.Item;
        }

        public void DelPosDrug(PostoperativeAnalgesiaElement ctrl)
        {
            if (CtrlList.Count > 1)
            {
                CtrlList.Remove(ctrl);
                PostoperativeAnalgesia.ItemList.Remove(ctrl.Item);
            }
        }
        #endregion

        #region EventHandlers
        public void SetPosPatientDrug()
        {
            CtrlList.Clear();
            _ztfsSelect = PostoperativeAnalgesia.Ztfs;
            if (PostoperativeAnalgesia.ItemList.Count < 1)
            {
                AddPosCtrl();
            }
            else
            {
                for (int i = 0; i < PostoperativeAnalgesia.ItemList.Count; i++)
                {
                    CreateMedicineTtem(i, PostoperativeAnalgesia.ItemList[i]);
                }
            }
        }
        

        public bool SavePosPatientDrug()
        {
            ShztXianShi.Clear();

            TextBlock ztfs = new TextBlock();
            ztfs.Text = "镇痛方式:" + PostoperativeAnalgesia.Ztfs;
            ShztXianShi.Add(ztfs);
            TextBlock ccd = new TextBlock();
            if (!string.IsNullOrEmpty(PostoperativeAnalgesia.Ccd1) || !string.IsNullOrEmpty(PostoperativeAnalgesia.Ccd2))
            {
                ccd.Text = "穿刺点:" + PostoperativeAnalgesia.Ccd1 + "与" + PostoperativeAnalgesia.Ccd2;
                ShztXianShi.Add(ccd);
            }
            TextBlock Dgsd = new TextBlock();
            if (!string.IsNullOrEmpty(PostoperativeAnalgesia.Dgsd))
            {
                Dgsd.Text = "导管深度:" + PostoperativeAnalgesia.Dgsd + "cm";
                ShztXianShi.Add(Dgsd);
            }
            TextBlock Ydzl = new TextBlock();
            Ydzl.Text = "药袋总量:" + PostoperativeAnalgesia.Ydzl + "ml";
            TextBlock Mxsls = new TextBlock();
            Mxsls.Text = "每小时流速:" + PostoperativeAnalgesia.Mxsls + "ml/h";
            TextBlock Pca = new TextBlock();
            Pca.Text = "PCA量:" + PostoperativeAnalgesia.Pca + "ml";
            TextBlock Sdsj = new TextBlock();
            Sdsj.Text = "锁定时间:" + PostoperativeAnalgesia.Sdsj + "min";

            ShztXianShi.Add(Ydzl);
            ShztXianShi.Add(Mxsls);
            ShztXianShi.Add(Pca);
            ShztXianShi.Add(Sdsj);
            foreach (var item in PostoperativeAnalgesia.ItemList)
            {
                List<ConfigDrugsModel> DrugsList = DataManager.Instance.ConfigDrugsModelList.Where(O => O.Deleted == false && O.Code != null && O.Code.Equals(item.Code)).ToList();
                if (DrugsList == null || DrugsList.Count < 1)
                {
                    item.Name = item.Code;
                }
                else
                {
                    item.Name = DrugsList.First().Name;
                }
                TextBlock dory = new TextBlock();
                dory.Text = item.Name + ":" + item.Dose + item.Unit;
                ShztXianShi.Add(dory);
            }
            //DataManager.Instance.CurrentPatientLink.PostoperativeAnalgesia = PostoperativeAnalgesia;
            PatientLinkService.Instance.AddUpdate(DataManager.Instance.CurrentPatientLink);
            return true;
        }

        public void DeletePosPatientDrug()
        {
            ShztXianShi.Clear();
        }
        public void Excute(string commond)
        {
            DataManager.Instance.IsNotNeedSaveData = false;
            switch (commond)
            {
                case "Delete":
                    {
                        DeletePosPatientDrug();
                        DataManager.Instance.CurrentPatientLink.PostoperativeAnalgesia.IsDisplay = false;
                        DrugAndEventWindow.Instance.CusContent = PostoperativeAnalgesiaCtrl.Instance;
                        DrugAndEventWindow.Instance.CloseWin(false);
                    }
                    break;
                case "Cancel":
                    {
                        PostoperativeAnalgesia = PostoperativeAnalgesiaCopy.ObjectToJsonM().JsonToObjectM<PostoperativeAnalgesiaModel>();
                        DrugAndEventWindow.Instance.CusContent = PostoperativeAnalgesiaCtrl.Instance;
                        DrugAndEventWindow.Instance.CloseWin(false);
                    }
                    break;
                case "Save":
                    {
                        DataManager.Instance.CurrentPatientLink.PostoperativeAnalgesia.IsDisplay = true;
                        if (SavePosPatientDrug())
                        {
                            DrugAndEventWindow.Instance.CusContent = PostoperativeAnalgesiaCtrl.Instance;
                            DrugAndEventWindow.Instance.CloseWin(false);
                        }
                    }
                    break;
            }
        }
        public void SelectSinceFilling()
        {
            CtrlList.Clear();
            ItemModel item1 = new ItemModel();
            ItemModel item2 = new ItemModel();
            ItemModel item3 = new ItemModel();
            ItemModel item4 = new ItemModel();
            List<ItemModel> itemLists = new List<ItemModel>();
            PostoperativeAnalgesia.Ztfs = ZtfsSelect;
            PostoperativeAnalgesia.ItemList = new List<ItemModel>();
            if ("PCIA".Equals(ZtfsSelect))
            {
                itemLists.Clear();
                item1.Name = "舒芬太尼";
                item1.Dose = "50";
                item1.Unit = "ug";
                item1.Code = "drug_203";
                item2.Name = "喷他佐辛";
                item2.Dose = "120";
                item2.Unit = "mg";
                item2.Code = "drug_197";
                item3.Name = "昂丹司琼";
                item3.Dose = "16";
                item3.Unit = "mg";
                item3.Code = "drug_204";
                item4.Name = "生理盐水";
                item4.Dose = "88";
                item4.Unit = "ml";
                item4.Code = "drug_206";
                itemLists.Add(item1);
                itemLists.Add(item2);
                itemLists.Add(item3);
                itemLists.Add(item4);
                for (int i = 0; i < itemLists.Count; i++)
                {
                    PostoperativeAnalgesia.ItemList.Add(CreateMedicineTtem(i, itemLists[i]));
                }
                PostoperativeAnalgesia.Ztfs = "PCIA";
                PostoperativeAnalgesia.Ccd1 = "";
                PostoperativeAnalgesia.Ccd2 = "";
                PostoperativeAnalgesia.Dgsd = "";
                PostoperativeAnalgesia.Ydzl = "100";
                PostoperativeAnalgesia.Mxsls = "2";
                PostoperativeAnalgesia.Pca = "0.5";
                PostoperativeAnalgesia.Sdsj = "15";
            }
            else if ("PCEA".Equals(ZtfsSelect))
            {
                itemLists.Clear();
                item1.Name = "罗哌卡因";
                item1.Dose = "300";
                item1.Unit = "mg";
                item1.Code = "drug_66";
                item2.Name = "喷他佐辛";
                item2.Dose = "90";
                item2.Unit = "mg";
                item2.Code = "drug_197";
                item3.Name = "生理盐水";
                item3.Dose = "70";
                item3.Unit = "ml";
                item3.Code = "drug_206";
                itemLists.Add(item1);
                itemLists.Add(item2);
                itemLists.Add(item3);
                for (int i = 0; i < itemLists.Count; i++)
                {
                    PostoperativeAnalgesia.ItemList.Add(CreateMedicineTtem(i, itemLists[i]));
                }
                PostoperativeAnalgesia.Ztfs = "PCEA";
                PostoperativeAnalgesia.Ccd1 = "L3";
                PostoperativeAnalgesia.Ccd2 = "L4";
                PostoperativeAnalgesia.Dgsd = "4";
                PostoperativeAnalgesia.Ydzl = "100";
                PostoperativeAnalgesia.Mxsls = "2";
                PostoperativeAnalgesia.Pca = "0.5";
                PostoperativeAnalgesia.Sdsj = "15";

            }
            else if ("PCNA".Equals(ZtfsSelect))
            {
                itemLists.Clear();
                item1.Name = "罗哌卡因";
                item1.Dose = "200";
                item1.Unit = "mg";
                item1.Code = "drug_66";
                item2.Name = "生理盐水";
                item2.Dose = "80";
                item2.Unit = "ml";
                item2.Code = "drug_206";
                itemLists.Add(item1);
                itemLists.Add(item2);
                for (int i = 0; i < itemLists.Count; i++)
                {
                    PostoperativeAnalgesia.ItemList.Add(CreateMedicineTtem(i, itemLists[i]));
                }
                PostoperativeAnalgesia.Ztfs = "PCNA";
                PostoperativeAnalgesia.Ccd1 = "";
                PostoperativeAnalgesia.Ccd2 = "";
                PostoperativeAnalgesia.Dgsd = "4";
                PostoperativeAnalgesia.Ydzl = "100";
                PostoperativeAnalgesia.Mxsls = "2";
                PostoperativeAnalgesia.Pca = "0.5";
                PostoperativeAnalgesia.Sdsj = "15";
            }
            else
            {
                if (PostoperativeAnalgesia != null)
                {
                    PostoperativeAnalgesia.ItemList.Add(new ItemModel());
                    PostoperativeAnalgesia.Ccd1 = "";
                    PostoperativeAnalgesia.Ccd2 = "";
                    PostoperativeAnalgesia.Dgsd = "";
                    PostoperativeAnalgesia.Ydzl = "";
                    PostoperativeAnalgesia.Mxsls = "";
                    PostoperativeAnalgesia.Pca = "";
                    PostoperativeAnalgesia.Sdsj = "";
                }
            }
            //DataManager.Instance.CurrentPatientLink.PostoperativeAnalgesia = PostoperativeAnalgesia;
            //PatientLinkService.Instance.AddUpdate(DataManager.Instance.CurrentPatientLink);
        }
        #endregion


    }
}
